/***************************************************************************
 *   Copyright (C) 2009 by Bernhard Neuhofer   *
 *   mail@bneu.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
using namespace std;

#include <iostream>
#include "sourceLp.h"
#include <limits>
#include "source.h"
#include "../tools/log.h"


  /**
   * @see AbstractLp#doStep
   */
  void SourceLp::doStep()
{
	if (getLvt()<getGvtStats()->getGvt()+50) //Bremse
	{
		//Update Output collector
		getSmOutput()->incSteps();

		//Snapshot the Sms relevant gvtStats
		getStepStats()->setColor(getGvtStats()->getColor());
		getStepStats()->setTmin(numeric_limits<double>::max());
		getStepStats()->setMd(true,getGvtStats()->getMd(true));
		getStepStats()->setMd(false,getGvtStats()->getMd(false));

		FILE_LOG(logDEBUG)<<"=========> doStep of LP  "<<getId()<<"   called"<<endl;
		FILE_LOG(logDEBUG)<<"LP "<<getId()<<" generates message:"<<endl;

		//Call the associated source model entity to generate one or more messages
		((Source*)getAssocMe())->generate();

		//Report changes back to SM
		getGvtStats()->setMd(true,getStepStats()->getMd(true));
		getGvtStats()->setMd(false,getStepStats()->getMd(false));
		if (getGvtStats()->getTmin()>getStepStats()->getTmin())
			getGvtStats()->setTmin(getStepStats()->getTmin());
	}
	
}

  /**
   * @see AbstractLp#sendMessage
   * @param  
   */
  void SourceLp::sendMessage(Message* message)
{

	//color the message in the stepColor
	message->setColor(getStepStats()->getColor());

	getAssocMh()->sendMessage(message);
	_lvt=message->getTimestamp();

	//Update Output collector
	getSmOutput()->incSent();

	//GVT algo: log sent message
	getStepStats()->incMd(getStepStats()->getColor());

	if (message->getTimestamp()<getStepStats()->getTmin())
		getStepStats()->setTmin(message->getTimestamp());

}

/**
 * @see AbstractLp#getLvt
 * @return 
 */
double SourceLp::getLvt()

{
	return _lvt;
}